package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils;

import android.content.ContentResolver;
import android.net.Uri;
import android.util.Log;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;

public class ProductUtils {
    public static Integer importFromTextFile(List<Product> products, ContentResolver contentResolver, Uri uri) throws IOException {
        int lineNumber = 0;

        InputStream inputStream = contentResolver.openInputStream(uri);
        if (inputStream == null) {
            throw new IOException("Unable to open InputStream from Uri");
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = br.readLine()) != null) {
            lineNumber++;
            String[] parts = line.split("[,，;\\s]+");
            if (parts.length != 4) {
                products.clear();
                return lineNumber;
            }

            String productId = UUID.randomUUID().toString();

            try {
                products.add(new Product(
                        productId,
                        parts[0],
                        ImageUtils.bitmap2Blob(QRCodeUtils.generateQRCode(productId, 300, 300)),
                        parts[1],
                        Double.parseDouble(parts[2]),
                        parts[3],
                        0
                ));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return -1;
    }

    public static Integer importFromExcelFile(List<Product> products, ContentResolver contentResolver, Uri uri) throws IOException {
        int lineNumber = 0;

        InputStream inputStream = contentResolver.openInputStream(uri);
        if (inputStream == null) {
            throw new IOException("Unable to open InputStream from Uri");
        }

        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);

        for (Row row : sheet) {
            lineNumber++;
            if (row.getRowNum() == 0) continue;

            if (row.getPhysicalNumberOfCells() != 4) {
                products.clear();
                workbook.close();
                return lineNumber;
            }

            try {
                // 获取单元格内容并校验
                String name = row.getCell(0).getStringCellValue();
                String category = row.getCell(1).getStringCellValue();
                double price;
                String formattedDate;

                // 校验 price 是否是数值
                if (row.getCell(2).getCellType() == CellType.NUMERIC) {
                    price = row.getCell(2).getNumericCellValue();
                } else {
                    products.clear();
                    workbook.close();
                    return lineNumber;
                }

                if (row.getCell(3).getCellType() == CellType.NUMERIC) {
                    Date date = row.getCell(3).getDateCellValue();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
                    formattedDate = dateFormat.format(date);
                } else {
                    products.clear();
                    workbook.close();
                    return lineNumber;
                }

                String productId = UUID.randomUUID().toString();

                products.add(new Product(
                        productId,
                        name,
                        ImageUtils.bitmap2Blob(QRCodeUtils.generateQRCode(productId, 300, 300)),
                        category,
                        price,
                        formattedDate,
                        0
                ));
            } catch (Exception e) {
                e.printStackTrace();
                products.clear();
                workbook.close();
                return lineNumber;
            }
        }

        workbook.close();
        return -1;
    }
}
